package com.lq.schoolshop.controller.before;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lq.schoolshop.bean.Result;
import com.lq.schoolshop.bean.StatusCode;
import com.lq.schoolshop.pojo.Expand.StoreAndGoods;
import com.lq.schoolshop.pojo.Goods;
import com.lq.schoolshop.pojo.Store;
import com.lq.schoolshop.service.GoodsService;
import com.lq.schoolshop.service.StoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author
 * @Date2021/1/8 13:12
 * @Version V1.0
 * 商品列表页功能
 **/
@Api(value = "商城前台功能--商品列表页模块",tags = "商城前台功能--商品列表页模块")
@RequestMapping("orderlist")
@Controller
public class ForGoodsListController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private StoreService storeService;

    //1.根据店铺ID显示当前店铺
    @ApiImplicitParam(name = "soreid",value = "店铺ID",dataType = "String",required = true,paramType = "query")
    @ApiOperation(value = "根据店铺ID显示当前店铺",notes = "显示根据ID查到的店铺信息")
    @ResponseBody
    @RequestMapping(value = "/ShowAllStoreBystoreid",method = RequestMethod.GET)
    public Result ShowAllStoreBystoreid(@RequestParam("soreid") int soreid){
        Result result = null;
        if(soreid<=0){
            result = new Result(false,StatusCode.ERROR,"失败");
        }else{
            List<Store> stores = storeService.selById(soreid);
            if(stores!=null && stores.size()>0){
                result = new Result(true,StatusCode.OK,"返回店铺信息成功",stores);
            }else{
                result = new Result(false,StatusCode.OK,"未加载到店铺信息");
            }
        }
        return result;
    }

    //2.根据店铺ID显示当前店铺所有商品
    @ApiImplicitParam(name = "soreid",value = "店铺ID",dataType = "String",required = true,paramType = "query")
    @ApiOperation(value = "根据店铺ID显示当前店铺所有商品",notes = "显示根据ID查到的店铺信息的所有商品信息分页显示")
    @ResponseBody
    @RequestMapping(value = "/ShowAllOrderBystoreid",method = RequestMethod.GET)
    public Result ShowAllOrderBystoreid(@RequestParam("soreid") String soreid, @RequestParam("pageNum") String pageNum,@RequestParam("pageSize") String pageSize){
        Result result = null;
        System.out.println("加载店铺时候的ID："+soreid);
        int pageNumint = Integer.parseInt(pageNum);
        int pageSizeint = Integer.parseInt(pageSize);
        int soreidint = Integer.parseInt(soreid);
        PageHelper.startPage(pageNumint,pageSizeint);
        List<Goods> goods = goodsService.selByStoreid(soreidint);
        PageInfo pageInfo = new PageInfo(goods);
        System.out.println();
        if(goods!=null && goods.size()>0){
            result = new Result(true,StatusCode.OK,"成功加载分页商品信息",pageInfo);
        }else {
            result = new Result(false,StatusCode.OK,"未查到该商品信息",pageInfo);
        }
        return result;
    }
}
